﻿namespace Nello.Web
{
    using System;
    using System.Data;
    using System.Web.UI.WebControls;

    public class MenuWrapper
    {
        private DataTable datasource;
        private string fieldNames;
        private Menu menu;
        private string parentIdField;
        private string rootId;
        private string textField;
        private string urlField;
        private string valueField;

        public MenuWrapper()
        {
            this.rootId = "0";
        }

        public MenuWrapper(Menu menu, DataTable datasource)
        {
            this.rootId = "0";
            this.menu = menu;
            this.datasource = datasource;
        }

        private void InitMenu(string parentid, MenuItemCollection items)
        {
            DataView dv = new DataView();
            if (this.datasource.TableName == "")
            {
                this.datasource.TableName = "NewTable";
            }
            dv.Table = this.datasource;
            dv.RowFilter = this.parentIdField + "='" + parentid + "'";
            foreach (DataRowView drv in dv)
            {
                MenuItem item = new MenuItem();
                item.Value = drv[this.valueField].ToString();
                item.Text = drv[this.textField].ToString();
                if (this.urlField != "")
                {
                    item.NavigateUrl = drv[this.urlField].ToString();
                }
                items.Add(item);
                this.InitMenu(item.Value, item.ChildItems);
            }
        }

        public void Render()
        {
            this.menu.Items.Clear();
            string[] ss = this.fieldNames.Split(new char[] { ',' });
            if (ss.Length < 3)
            {
                throw new Exception("FieldNames属性设置的格式为parentid,value,text,url或parentid,value,text");
            }
            this.parentIdField = ss[0];
            this.valueField = ss[1];
            this.textField = ss[2];
            if (ss.Length == 3)
            {
                this.urlField = "";
            }
            else
            {
                this.urlField = ss[3];
            }
            this.InitMenu(this.rootId, this.menu.Items);
        }

        public DataTable Datasource
        {
            get
            {
                return this.datasource;
            }
            set
            {
                this.datasource = value;
            }
        }

        public string FieldNames
        {
            get
            {
                return this.fieldNames;
            }
            set
            {
                this.fieldNames = value;
            }
        }

        public Menu MenuControl
        {
            get
            {
                return this.menu;
            }
            set
            {
                this.menu = value;
            }
        }

        public string RootId
        {
            get
            {
                return this.rootId;
            }
            set
            {
                this.rootId = value;
            }
        }
    }
}

